<?php
namespace Admin\Controller;
class NavsController extends AuthController{

    /*
     * 链接表*/
    public function index()
    {
        $this->page_title=" 管理菜单";
        $this->navs=M('navs')->order('sort desc')->select();
        //;p($this->link);die();
        $this->display();
    }

    /*
     * 添加链接*/
    public function add()
    {
        $this->page_title=" 添加菜单";
        $this->display();
    }
    /*
     *  添加处理*/
    public function addHandler(){
     //        p($_POST);die();
        $rules=[
            ['name','require','连接名称必填'],
            ['name','','连接名称不能重复',0,'unique',1],
            ['url','require','链接地址必填'],
        ];
        if(!M('Navs')->validate($rules)->create()) $this->error(M('navs')->getError());
        if(!M('Navs')->add(I('post.'))) $this->error('添加链接失败');
        $this->success('添加成功',U('navs/index'));
        }

        /*
         * 删除链接*/
        public function delete($id){
            if(M('navs')->where(array('id'=>$id))->delete())
            {
                $data=['status'=>1,'message'=>'  删除成功'];
            }else{
                $data=['status'=>0,'message'=>'  删除失败'];
            }
            $this->ajaxReturn($data);
        }
        /*
         * 编辑链接*/
        public function edit($id)
        {
            $this->page_title=" 链接编辑";
            $this->nav=M('navs')->where(array('id'=>$id))->find();
            //p($this->link);die();
            $this->display();
        }
        /*
         * 处理编辑数据*/
        public function saveHandler()
        {
           // if(!M('Links')->save(I('post.'))) $this->error('编辑失败');
            if(!M('navs')->save(I('post.'))) $this->error('编辑失败');

            $this->success('编辑导航成功',U('navs/index'));
        }

    /*
* 更该排序
* */
    public  function changeSort(){
        //dump($_POST);die();
        if(M('navs')->where(array('id'=>I('post.id')))->save(array('sort'=>I('post.sort'))))
        {
            $data=['status'=>1,'message'=>' 排序成功'];
        }
        $this->ajaxReturn($data);
    }

}